package com.shuai.baibi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuai.baibi.common.BaseResponse;
import com.shuai.baibi.common.ErrorCode;
import com.shuai.baibi.exception.BusinessException;
import com.shuai.baibi.model.entity.ChartAnnex;
import com.shuai.baibi.model.vo.ChartAnnexVO;
import com.shuai.baibi.service.ChartAnnexService;
import com.shuai.baibi.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/annex")
public class ChartAnnexController {
    @Autowired
    private ChartAnnexService chartAnnexService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseResponse<ChartAnnexVO> getAnnexBaseInfoByChartId(Long chartId) {
        if (chartId == null || chartId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LambdaQueryWrapper<ChartAnnex> chartAnnexWrapper = new LambdaQueryWrapper<>();
        chartAnnexWrapper.eq(ChartAnnex::getChartId, chartId);
        chartAnnexWrapper.eq(ChartAnnex::getIsLasted, ChartAnnex.Y);
        ChartAnnex chartAnnex = chartAnnexService.getOne(chartAnnexWrapper);
        if (chartAnnex == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息附件基本信息不存在");
        }
        ChartAnnexVO chartAnnexVO = new ChartAnnexVO();
        BeanUtils.copyProperties(chartAnnex, chartAnnexVO);
        return ResultUtils.success(chartAnnexVO);
    }
}
